//
//  SCWishViewModel.m
//  Mutal
//
//  Created by TonyStart on 16/10/20.
//  Copyright © 2016年 com.wx. All rights reserved.
//

#import "SCWishViewModel.h"
#import "SCNetworkTool.h"
@implementation SCWishViewModel

-(NSMutableArray<SCWishModel *> *)wishArray{
    if (!_wishArray) {
        _wishArray =[NSMutableArray array];
    }
    return _wishArray;
}
-(void)requestWishsData:(void(^)())finish{
    
    NSString *url = urlAndParamBuild(@"Appapi", @"wish", @"index");
    [[SCNetworkTool shared] requestDataWithMethod:GET url:url finish:^(id data, NSError *err,NSInteger code) {
        if (data) {
            for (NSDictionary *dict in data) {
                SCWishModel *wishModel = [SCWishModel wishModelWithDict:dict];
                [self.wishArray addObject:wishModel];
            }
            finish();
        }
    }];
}
-(void)requestWishsDataWithPage:(NSInteger)page finish:(void(^)())finish{
    NSDictionary *param = @{@"g":@"Appapi",@"m":@"wish",@"a":@"index",@"page":[NSNumber numberWithInteger:page]};
    NSString *url = urlMakeWithDict(param);


    [[SCNetworkTool shared] requestDataWithMethod:GET url:url finish:^(id data, NSError *err,NSInteger code) {
        if (data) {
          
            for (NSDictionary *dict in data) {
                SCWishModel *wishModel = [SCWishModel wishModelWithDict:dict];
                [self.wishArray addObject:wishModel];
            }
            finish();
        }
        
    }];
}
//首页推荐WISH
-(void)requestWishsDataIS_TJ:(NSInteger)is_tj finish:(void(^)())finish{
    NSDictionary *param = @{@"g":@"Appapi",@"m":@"wish",@"a":@"index",@"is_tj":[NSNumber numberWithInteger:is_tj]};
    NSString *url = urlMakeWithDict(param);


    [[SCNetworkTool shared] requestDataWithMethod:GET url:url finish:^(id data, NSError *err,NSInteger code) {
        if (data) {

            for (NSDictionary *dict in data) {
                SCWishModel *wishModel = [SCWishModel wishModelWithDict:dict];
                [self.wishArray addObject:wishModel];
            }
            finish();
        }
        
    }];
}
//根据type查找
-(void)requestWishsDataWithType:(NSInteger)type loadType:(refReshLoadType)loadType page:(NSInteger)page finish:(void(^)(BOOL isLoad))finish{
  
    NSNumber *pageNumer = [NSNumber numberWithInteger:page];
    
    NSDictionary *param = @{@"g":@"Appapi",@"m":@"wish",@"a":@"index",@"wish_type":[NSNumber numberWithInteger:type],@"page":pageNumer};
   
    NSString *url = urlMakeWithDict(param);
    NSLog(@"%@",url);
    [[SCNetworkTool shared] requestDataWithMethod:GET url:url finish:^(id data, NSError *err,NSInteger code) {
        if (data) {
            NSMutableArray<SCWishModel*> *tempArray = [NSMutableArray array];
            for (NSDictionary *dict in data) {
                SCWishModel *wishModel = [SCWishModel wishModelWithDict:dict];
                [tempArray addObject:wishModel];
            }
            //加载方式  上拉加载数据 还是下拉加载数据
            switch (loadType) {
                case dropLoad://下拉 加载最新的数据
                    self.wishArray = tempArray;
                    
                    break;
                case pullLoad://上拉 在之前的数据上面再加载数据
                    if (tempArray.count == 0) {
                        finish(NO);
                        return;
                    }
                    self.wishArray = [self.wishArray arrayByAddingObjectsFromArray:tempArray].mutableCopy;
                    break;
                default:
                    break;
            }
            
            finish(YES);
        }
        
    }];
}
//根据类别查找
-(void)requestWishsDataWithCat:(NSInteger)cat finish:(void(^)())finish{
    NSDictionary *param = @{@"g":@"Appapi",@"m":@"wish",@"a":@"index",@"wish_cat":[NSNumber numberWithInteger:cat]};
    NSString *url = urlMakeWithDict(param);
    
    [[SCNetworkTool shared] requestDataWithMethod:GET url:url finish:^(id data, NSError *err,NSInteger code) {
        if (data) {
            
            for (NSDictionary *dict in data) {
                SCWishModel *wishModel = [SCWishModel wishModelWithDict:dict];
                [self.wishArray addObject:wishModel];
            }
            
            finish();
        }
        
    }];
}
//关键字查找
-(void)requestWishsDataWithSearch:(NSString*)search finish:(void(^)())finish{
    NSDictionary *param = @{@"g":@"Appapi",@"m":@"wish",@"a":@"index",@"search":search};
    NSString *url = urlMakeWithDict(param);
    
    [[SCNetworkTool shared] requestDataWithMethod:GET url:url finish:^(id data, NSError *err,NSInteger code) {
        if (data) {
            
            for (NSDictionary *dict in data) {
                SCWishModel *wishModel = [SCWishModel wishModelWithDict:dict];
                [self.wishArray addObject:wishModel];
            }
            finish();
        }
        
    }];
}
//最新愿望和热点愿望查询
-(void)requestWishsDataWithHot:(NSString*)is_hot loadType:(refReshLoadType)loadType page:(NSInteger)page finish:(void(^)(BOOL isLoad))finish{
    NSNumber *pageNumer = [NSNumber numberWithInteger:page];
    NSDictionary *param = @{@"g":@"Appapi",@"m":@"wish",@"a":@"index",@"is_hot":is_hot,@"page":pageNumer};
    NSString *url = urlMakeWithDict(param);
    NSLog(@"%@",url);
    [[SCNetworkTool shared] requestDataWithMethod:GET url:url finish:^(id data, NSError *err,NSInteger code) {
        if (data) {
            NSMutableArray *tempArray = [NSMutableArray array];
            for (NSDictionary *dict in data) {
                SCWishModel *wishModel = [SCWishModel wishModelWithDict:dict];
               
                [tempArray addObject:wishModel];
            }
            //加载方式  上拉加载数据 还是下拉加载数据
            switch (loadType) {
                case dropLoad://下拉 加载最新的数据
                    self.wishArray = tempArray;
                    
                    break;
                case pullLoad://上拉 在之前的数据上面再加载数据
                    if (tempArray.count == 0) {
                        finish(NO);
                        return;
                    }
                    self.wishArray = [self.wishArray arrayByAddingObjectsFromArray:tempArray].mutableCopy;
                    break;
                default:
                    break;
            }
            
            finish(YES);
        }
        
    }];
}
//我的愿望
-(void)requestWishsDataByUserId:(NSString*)userId finish:(void(^)())finish{
    NSDictionary *param = @{@"g":@"Appapi",@"m":@"wish",@"a":@"index",@"user_id":userId};

    NSString *url = urlMakeWithDict(param);
    NSLog(@"%@",url);
    [[SCNetworkTool shared] requestDataWithMethod:GET url:url finish:^(id data, NSError *err,NSInteger code) {
        if (data) {
            
            for (NSDictionary *dict in data) {
                SCWishModel *wishModel = [SCWishModel wishModelWithDict:dict];
                [self.wishArray addObject:wishModel];
            }
            finish();
        }
        
    }];
}
@end
